/*
发牌器节点，在游戏场景中用来给玩家发牌
*/
var gameData = require("gameData");
var confige = require("confige")

cc.Class({
    extends: cc.Component,

    properties: {
        cardItem:{
            default:null,
            type:cc.Node
        },
    },

    // use this for initialization
    onLoad: function () {

    },

    onInit:function(gameType){
        this.disAllUser = false;
        if(gameType)
            this.disAllUser = true;
        this.cardItemList = {};
        this.cardItemCount = 0;
        this.mainPlayerScale = 1;
        this.otherPlayerScale = 1;
        
        this.playerActiveList = {};
        this.handCardPosList =  {};
        this.paigowhandCardPosList =  {};
        this.handCardList = {};
        this.curPlayerCount = confige.playerMax;

        for(var i=0;i<this.curPlayerCount;i++)
        {
            this.handCardList[i] = gameData.gamePlayerNode.playerListNew[i];
        }

        for(var i=0;i<this.curPlayerCount;i++)
        {
            this.handCardPosList[i] = {};
            this.playerActiveList[i] = false;
            for(var j=0;j<5;j++)
            {
                var curCardBackNode = this.handCardList[i].cardsBack[j];
                var newVec1 = curCardBackNode.parent.convertToWorldSpaceAR(cc.v2(curCardBackNode.x,curCardBackNode.y));
                var newVec2 = this.node.convertToNodeSpace(cc.v2(newVec1.x,newVec1.y));
                this.handCardPosList[i][j] = {x:newVec2.x,y:newVec2.y};
            }
        }
       
        this.disRoundCount = 0;
    },

    resetCardList:function(){
        for(var i=0;i<this.curPlayerCount;i++){
            this.playerActiveList[i] = false;
        }
        for(var i=0;i<this.cardItemCount;i++)
            this.cardItemList[i].destroy();
        this.cardItemCount = 0;
        this.disRoundCount = 0;
    },

    activePlayer:function(index){
        console.log("activePlayer!!!!")
        this.playerActiveList[index] = true;
    },

    deActivePlayer:function(index){
        this.playerActiveList[index] = false;
    },

    disCardOneRound:function(){
        console.log("disCardOneRound!!!!!!!!!!!!!!!!!")
        var disCount = 0;
        var playerCount = 0;
        var chairList = [];
        //发牌按逆时针顺序
        var playerSequence = [0,8,4,6,2,1,5,3,7]
        console.log(this.playerActiveList)
        for(var i in this.playerActiveList){
            if(this.playerActiveList[playerSequence[i]]){
                playerCount ++;
                chairList.push(playerSequence[i]);
            }
        }
        this.disRoundCount ++;
        var curRoundCount = this.disRoundCount - 1;
        var callBack = function(){
            // if(this.disAllUser || this.playerActiveList[disCount] == true)
            // {
                var curChair = chairList[disCount];
                var newCardItem = cc.instantiate(this.cardItem);
                this.node.addChild(newCardItem);
                newCardItem.active = true;
                newCardItem.x = -545;
                newCardItem.y = 190;
                this.cardItemList[this.cardItemCount] = newCardItem;
                this.cardItemCount ++;
                window.DKTools.audioEG.playEffect("tips_fapai");
                var action1 = cc.moveTo(0.2, cc.p(this.handCardPosList[curChair][curRoundCount].x, this.handCardPosList[curChair][curRoundCount].y));
                var scaleNum = 1;
                if(curChair == 0)
                    scaleNum = 1.5;
                else
                    scaleNum = 0.7;
                var action2 = cc.scaleTo(0.2,scaleNum);
                var self = this
                var action3 = (function(curChair,curRoundCount,self) {
                   return cc.callFunc(function () {
                    newCardItem.active = false;
                    self.handCardList[curChair].showCardBackWithIndex(curRoundCount);
                }, self)
                })(curChair,curRoundCount,self)

                newCardItem.runAction(cc.sequence(cc.spawn(action1,action2), cc.delayTime(0.1),action3));
            // }            
            disCount ++;
            if(disCount == playerCount)
            {
                // this.unschedule(callBack);
                window.protectedUnschedule(this,callBack);
                if(confige.roomData.bankerMode != 8)
                {
                    gameData.gameMainScene.alldisCardover();
                }
            }
        };

        this.schedule(callBack,0.75);
    },

    disCardWithRoundTime:function(rountTime){
        var callTime = 0;
        var callMax = rountTime;
        var callBack2 = function(){
            this.disCardOneRound();
            callTime ++;
            if(callTime == callMax){
                window.protectedUnschedule(this,callBack2);
                // this.unschedule(callBack2);
            }
        };
        this.schedule(callBack2,0.05);
    },

    disPaiGow:function(){
        var playerCount = 0;
        var chairList = [];
        //发牌按逆时针顺序
        var playerSequence = [0,8,4,6,2,1,5,3,7]
        for(var i in this.playerActiveList){
            if(this.playerActiveList[playerSequence[i]]){
                playerCount ++;
                chairList.push(playerSequence[i]);
            }
        }
        
        console.log("disPaiGow!!!!!!!!!!!!!!!!!");
        console.log(this.playerActiveList)
        console.log(chairList)
        var self = this;

        var disCount = 0;
        var callBack = function(){
            // if(this.disAllUser || this.playerActiveList[disCount] == true)
            // {
                var curChair = chairList[disCount];
                var newCardItem = cc.instantiate(this.cardItem);
                this.node.addChild(newCardItem);
                newCardItem.active = true;
                newCardItem.x = -545;
                newCardItem.y = 190;
                this.cardItemList[this.cardItemCount] = newCardItem;
                this.cardItemCount ++;

                window.DKTools.audioEG.playEffect("tips_disCard");
                var action1 = cc.moveTo(0.3, cc.p(this.handCardPosList[curChair][0].x, this.handCardPosList[curChair][0].y));
                var scaleNum = 1;
                var action2 = cc.scaleTo(0.3,scaleNum);
                var self = this
                var action3 = (function(curChair,self) {
                   return cc.callFunc(function () {
                    newCardItem.active = false;
                    self.handCardList[curChair].showCardBackWithIndex(0);
                }, self)
                })(curChair,self)

                newCardItem.runAction(cc.sequence(cc.spawn(action1,action2), cc.delayTime(0.1),action3));

                var newCardItem2 = cc.instantiate(this.cardItem);
                this.node.addChild(newCardItem2);
                newCardItem2.active = true;
                newCardItem2.x = -545;
                newCardItem2.y = 190;
                this.cardItemList[this.cardItemCount] = newCardItem2;
                this.cardItemCount ++;

                var action11 = cc.moveTo(0.3, cc.p(this.handCardPosList[curChair][1].x, this.handCardPosList[curChair][1].y));
                var scaleNum = 1;
                if(curChair == 0)
                    scaleNum = this.mainPlayerScale;
                else
                    scaleNum = this.otherPlayerScale;
                var action21 = cc.scaleTo(0.3,scaleNum);
                var self = this
                var action31 = (function(curChair,self) {
                   return cc.callFunc(function () {
                    newCardItem2.active = false;
                    self.handCardList[curChair].showCardBackWithIndex(1);
                }, self)
                })(curChair,self)

                newCardItem2.runAction(cc.sequence(cc.spawn(action11,action21), cc.delayTime(0.1),action31));
            // }            
            disCount ++;
            if(disCount == playerCount)
            {
                // this.unschedule(callBack);
                window.protectedUnschedule(this,callBack);
                gameData.gameMainScene.alldisCardover();
            }
        };

        this.schedule(callBack,0.3);
    },
});
